﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;
using System.Reflection;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.MessageInfo
{
    public partial class frmManageMessageBox : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private MstMessageEntity mSelectedMessage;
        #endregion

        #region Constructor
        public frmManageMessageBox()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbMessageLevel, "ML", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbShowMessageLevel, "ML", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colMESSAGE_LEVEL, "ML", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "MS", true);

            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbMessageStatus, "MS", true);
            cmbMessageStatus.SelectedValue = MessageStatus.MS01.ToString();

            cmbInboxType.SelectedIndex = 0;
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtMessageTitle.Text) == false)
            {
                condition += " AND M.MESSATE_TITLE LIKE '%" + txtMessageTitle.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtMessageBody.Text) == false)
            {
                condition += " AND M.MESSAGE_BODY LIKE '%" + txtMessageBody.Text + "%'";
            }
            if (txtMessageSender.Tag != null && txtMessageSender.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity sender = (MstPersonEntity)txtMessageSender.Tag;
                condition += " AND M.MESSAGE_SENDER = '" + sender.PersonId + "'";
            }
            if (dtpMessageDate.EditValue!=null&&dtpMessageDate.EditValue.ToString().Equals("0001-01-01 00:00:00")==false)
            {
                condition += " AND CONVERT(VARCHAR,M.MESSAGE_DATE,120) LIKE '%" + ((DateTime)dtpMessageDate.EditValue).ToString("yyyy-MM-dd") + "%'";
            }
            if (string.IsNullOrEmpty(cmbMessageLevel.Text) == false && cmbMessageLevel.SelectedValue != null)
            {
                condition += " AND M.MESSAGE_LEVEL = '" + cmbMessageLevel.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbMessageStatus.Text) == false && cmbMessageStatus.SelectedValue != null)
            {
                condition += " AND PM.STATUS = '" + cmbMessageStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "MESSAGE_DATE DESC";
            bool isInbox = true;
            if (cmbInboxType.Text == "发件箱")
            {
                isInbox = false;
            }
            DataTable dt = MstMessageBs.GetMstMessageByCondition(CommonParameter.CurrentUser.PersonId, condition, orderBy, rowCount, isInbox);
            //this.gvMain.SelectionChanged -= new System.EventHandler(this.gvMain_SelectionChanged);
            //gvMain.DataSource = dt;
            //this.gvMain.SelectionChanged += new System.EventHandler(this.gvMain_SelectionChanged);
        }
        private void InitMessageFields()
        {
            txtShowMessageTitle.Text = "";
            txtShowMessageDate.Text = "";
            txtShowMessageSender.Text = "";
            txtShowMessageReceiver.Text = "";
            cmbShowMessageLevel.SelectedIndex = -1;
            txtShowMessageBody.Text = "";
        }
        private void SetMessageFields()
        {
            if (mSelectedMessage == null)
            {
                return;
            }
            txtShowMessageTitle.Text = mSelectedMessage.MessateTitle;
            txtShowMessageDate.Text = mSelectedMessage.MessageDate.ToString("yyyy-MM-dd hh:mm:ss");
            txtShowMessageSender.Text = "";
            if (string.IsNullOrEmpty(mSelectedMessage.MessageSender) == false)
            {
                MstPersonEntity sender = MstPersonBs.GetMstPersonByID(mSelectedMessage.MessageSender);
                if (sender != null)
                {
                    txtShowMessageSender.Text = sender.NameCn;
                }
            }
            txtShowMessageReceiver.Text = "";
            if (string.IsNullOrEmpty(mSelectedMessage.MessageId) == false)
            {
                List<MstPersonMessageEntity> list = MstPersonMessageBs.GetMstPersonMessageByMessage(mSelectedMessage.MessageId);
                if (list != null)
                {
                    foreach (MstPersonMessageEntity entity in list)
                    {
                        MstPersonEntity receiver = MstPersonBs.GetMstPersonByID(entity.PersonId);
                        if (string.IsNullOrEmpty(txtShowMessageReceiver.Text))
                        {
                            txtShowMessageReceiver.Text = receiver.NameCn;
                        }
                        else
                        {
                            txtShowMessageReceiver.Text += "; " + receiver.NameCn;
                        }
                    }
                }
            }
            cmbShowMessageLevel.SetSelectedValue(mSelectedMessage.MessageLevel);
            txtShowMessageBody.Text = mSelectedMessage.MessageBody;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            BindDataGrid();
        }

        //private void gvMain_SelectionChanged(object sender, EventArgs e)
        //{
        //    if (gvMain.SelectedRows != null && gvMain.SelectedRows.Count > 0)
        //    {
        //        DataRowView dr = (DataRowView)gvMain.SelectedRows[0].DataBoundItem;
        //        mSelectedMessage = new MstMessageEntity(dr);
        //        SetMessageFields();
        //        //修改状态为已读
        //        MstPersonMessageEntity entity = MstPersonMessageBs.GetMstPersonMessage(mSelectedMessage.MessageId, CommonParameter.CurrentUser.PersonId);
        //        if (entity != null && entity.Status == MessageStatus.MS01.ToString())
        //        {
        //            entity.Status = MessageStatus.MS02.ToString();
        //            bool successful = MstPersonMessageBs.UpdateMstPersonMessage(entity);
        //            if (successful)
        //            {
        //                gvMain.SelectedRows[0].Cells["colSTATUS"].Value = MessageStatus.MS02.ToString();
        //            }
        //        }
        //    }
        //    else
        //    {
        //        mSelectedMessage = null;
        //        InitMessageFields();
        //    }
        //}

        //protected override void Click_Delete()
        //{
        //    if (gvMain.SelectedRows.Count > 0)
        //    {
        //        List<MstPersonMessageEntity> list = new List<MstPersonMessageEntity>();
        //        for (int i = 0; i < gvMain.SelectedRows.Count; i++)
        //        {
        //            DataRowView dr = (DataRowView)gvMain.SelectedRows[i].DataBoundItem;
        //            MstMessageEntity message = new MstMessageEntity(dr);
        //            MstPersonMessageEntity entity = MstPersonMessageBs.GetMstPersonMessage(message.MessageId, CommonParameter.CurrentUser.PersonId);
        //            if (entity.Status != MessageStatus.MS03.ToString())
        //            {
        //                entity.Status = MessageStatus.MS03.ToString();
        //                list.Add(entity);
        //            }
        //        }
        //        bool successful = MstPersonMessageBs.UpdateMstPersonMessage(list);
        //        if (successful)
        //        {
        //            BindDataGrid();
        //        }
        //    }
        //}

        //protected override void Click_Modify()
        //{
        //    if (gvMain.SelectedRows.Count > 0)
        //    {
        //        List<MstPersonMessageEntity> list = new List<MstPersonMessageEntity>();
        //        for (int i = 0; i < gvMain.SelectedRows.Count; i++)
        //        {
        //            DataRowView dr = (DataRowView)gvMain.SelectedRows[i].DataBoundItem;
        //            MstMessageEntity message = new MstMessageEntity(dr);
        //            MstPersonMessageEntity entity = MstPersonMessageBs.GetMstPersonMessage(message.MessageId, CommonParameter.CurrentUser.PersonId);
        //            if (entity.Status == MessageStatus.MS01.ToString())
        //            {
        //                entity.Status = MessageStatus.MS02.ToString();
        //                list.Add(entity);
        //            }
        //        }
        //        bool successful = MstPersonMessageBs.UpdateMstPersonMessage(list);
        //        if (successful)
        //        {
        //            BindDataGrid();
        //        }
        //    }
        //}

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_New()
        {
            frmMaintainMessageBox frm = new frmMaintainMessageBox();
            frm.ThisActionType = ActionType.New;
            frm.ShowDialog();
        }

        protected override void Click_View()
        {
            if (mSelectedMessage == null)
            {
                MessageBox.Show("请选择一个消息！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainMessageBox frm = new frmMaintainMessageBox(mSelectedMessage);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        //private void gvMain_CellClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex >= 0 && gvMain.Columns[e.ColumnIndex].Name == "colLink")
        //    {
        //        DataRowView dr = (DataRowView)gvMain.Rows[e.RowIndex].DataBoundItem;
        //        MstMessageEntity message = new MstMessageEntity(dr);
        //        if (string.IsNullOrEmpty(message.FormName))
        //        {
        //            return;
        //        }
        //        Assembly assDll = Assembly.GetExecutingAssembly();
        //        Form frm = (Form)assDll.CreateInstance(message.FormName);
        //        SecMenuEntity menu = SecMenuBs.GetMenuByFormName(message.FormName);
        //        if (menu != null)
        //        {
        //            frm.Text = menu.MenuName;
        //        }
        //        frmMain parentForm = (frmMain)this.MdiParent;
        //        parentForm.OpenForm(frm);
        //    }
        //}


        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {

        }

        private void txtMessageSender_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtMessageSender.Text = frm.SelectedPerson.NameCn;
                txtMessageSender.Tag = frm.SelectedPerson;
            }
            else
            {
                txtMessageSender.Text = "";
                txtMessageSender.Tag = null;
            }
        }

        #endregion

    }
}
